package com.xjscrm.console.service.customer.groupSend.impl;

import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.param.groupSend.BatchGroupSendJobDto;
import com.xjscrm.console.param.groupSend.BatchUpdateSleepRuleDto;
import com.xjscrm.console.param.groupSend.UpdateSleepRuleDto;
import com.xjscrm.console.service.customer.groupSend.BatchGroupSendService;
import com.xjscrm.console.service.customer.groupSend.GroupSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BatchGroupSendServiceImpl implements BatchGroupSendService {
    @Autowired
    private GroupSendService groupSendService;

    @Override
    public void batchUpdateSleepRule(BatchUpdateSleepRuleDto batchUpdateSleepRuleDto) {
        if (batchUpdateSleepRuleDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchUpdateSleepRuleDto.getUpdateSleepRuleDtoList() == null || batchUpdateSleepRuleDto.getUpdateSleepRuleDtoList().isEmpty()) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (UpdateSleepRuleDto updateSleepRuleDto : batchUpdateSleepRuleDto.getUpdateSleepRuleDtoList()) {
            try {
                updateSleepRuleDto.setMerId(batchUpdateSleepRuleDto.getMerId());
                groupSendService.updatesleeprule(updateSleepRuleDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void batchBreakGroupSendJob(BatchGroupSendJobDto batchGroupSendJobDto) {
        if (batchGroupSendJobDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchGroupSendJobDto.getIds() == null || batchGroupSendJobDto.getIds().isEmpty()) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long id : batchGroupSendJobDto.getIds()) {
            try {
                batchGroupSendJobDto.setId(id);
                groupSendService.breakGroupSendJob(batchGroupSendJobDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void batchContinueGroupSendJob(BatchGroupSendJobDto batchGroupSendJobDto) {
        if (batchGroupSendJobDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchGroupSendJobDto.getIds() == null || batchGroupSendJobDto.getIds().isEmpty()) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long id : batchGroupSendJobDto.getIds()) {
            try {
                batchGroupSendJobDto.setId(id);
                groupSendService.continueGroupSendJob(batchGroupSendJobDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void batchCancelGroupSendJob(BatchGroupSendJobDto batchGroupSendJobDto) {
        if (batchGroupSendJobDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchGroupSendJobDto.getIds() == null || batchGroupSendJobDto.getIds().isEmpty()) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long id : batchGroupSendJobDto.getIds()) {
            try {
                batchGroupSendJobDto.setId(id);
                groupSendService.cancelGroupSendJob(batchGroupSendJobDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void batchDeleGroupSendJob(BatchGroupSendJobDto batchGroupSendJobDto) {
        if (batchGroupSendJobDto.getMerId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (batchGroupSendJobDto.getIds() == null || batchGroupSendJobDto.getIds().isEmpty()) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long id : batchGroupSendJobDto.getIds()) {
            try {
                batchGroupSendJobDto.setId(id);
                groupSendService.deleGroupSendJob(batchGroupSendJobDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}